package cn.edu.aust.myexpri5app.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.apache.commons.lang3.StringUtils;

import java.util.List;

import cn.edu.aust.myexpri5app.R;
import cn.edu.aust.myexpri5app.model.UserInfo;

public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder>{
    private Context mContext;
    private List<UserInfo> userList;
    private LayoutInflater mInflater;

    //定义item的单击事件监听器和结构
    private ItemClickListener myItemClickListener;
    public interface ItemClickListener{
        public void onItemClick(int position);
    }

    public void setOnItemClickListener(ItemClickListener itemClickListener){
        this.myItemClickListener=itemClickListener;
    }

    public MyRecyclerAdapter(Context mContext, List<UserInfo> userList) {
        this.mContext = mContext;
        this.userList = userList;
        this.mInflater= LayoutInflater.from(mContext);
    }

    public MyRecyclerAdapter(Context mContext, List<UserInfo> userList, ItemClickListener listener) {
        this(mContext,userList);
        this.myItemClickListener=listener;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view=mInflater.inflate(R.layout.list_item,parent,false);
        MyViewHolder viewHolder=new MyViewHolder(view);
        return viewHolder;
    }

    public UserInfo getUser(int position){
        return userList.get(position);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        //实现item间隔显示不同底色
        if (position%2==0){
            holder.rlItem.setBackgroundColor(mContext.getResources().getColor(R.color.myWhite,mContext.getTheme()));
        }else{
            holder.rlItem.setBackgroundColor(mContext.getResources().getColor(R.color.myListColor,mContext.getTheme()));
        }

        UserInfo user=userList.get(position);
        holder.tvUserId.setText(user.getId());
        holder.etUsername.setText(user.getUsername());
        holder.etPassword.setText(user.getPassword());
        holder.etPhone.setText(user.getPhone());
        holder.etSex.setText(user.getSex());
        if (user.getHobbys()!=null && user.getHobbys().size()>0){
            String hobs="";
            for (String hob:user.getHobbys()){
                hobs+=hob+",";
            }
            hobs=hobs.substring(0,hobs.length()-1);
            holder.etHobbys.setText(hobs);
        }else{
            holder.etHobbys.setText("未知");
        }

        //调用item单击事件处理方法
        if (myItemClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    myItemClickListener.onItemClick(position);
                }
            });
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return userList.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder{
        RelativeLayout rlItem;
        TextView tvUserId;
        EditText etUsername;
        EditText etPassword;
        EditText etPhone;
        EditText etSex;
        EditText etHobbys;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            this.rlItem=(RelativeLayout)itemView.findViewById(R.id.rl_userInfo);
            this.tvUserId = (TextView)itemView.findViewById(R.id.tv_userId);
            this.etUsername=(EditText) itemView.findViewById(R.id.et_username);
            this.etPassword=(EditText) itemView.findViewById(R.id.et_password);
            this.etPhone=(EditText) itemView.findViewById(R.id.et_phone);
            this.etSex=(EditText) itemView.findViewById(R.id.et_sex);
            this.etHobbys=(EditText) itemView.findViewById(R.id.et_hobbys);
        }
    }
}
